import {
  getAcctHotSearch
} from '@/static/js/law.js';
import {
  getAcctSearch,
  readJsonFileContent
} from '@/static/js/search.js';
import {
  hightLightText, parseTimeDot
} from "@/utils/utils.js"
export default {
  data() {
    return {
      hightLightText,//高亮函数
      parseTimeDot,//时间格式化
      hotPlaceholderStr: '', //热门搜索
      isInputFocus: false, //是否聚焦
      keyword: '', //输入框的值
      historyList: [], //历史记录
      hotSearchList: [], //热门搜索
      isShowSearchList: false, //是否显示搜索结果
      isLoading: false, //是否正在加载
      page: 1,
      pageSize: 10,
      total: 0, //总条数
      status: 'more', //加载更多状态
      isRefreshing: false, //是否正在刷新
      searchList: [], //搜索结果JSON数据
      isSearch: false, //是否搜索过
      dataList: [], //搜索结果处理后数据
      tagStyle: {
        em: 'font-style: normal;color: #fa3534;'
      },//样式

      isShowPopup: false, //是否显示弹窗
      chooseId: ["name"],
      chooseLabel: "查标题",
      columns: [
        [
          {
            label: "查标题",
            id: ["name"],
          },
          {
            label: "查文章",
            id: ["info_content_text", "name"],
          },
          {
            label: "查文号",
            id: ["push_number"],
          },
          {
            label: "查时间",
            id: ["push_date"],
          },
        ],
      ],
      startDate: "开始时间",
      endDate: "结束时间",
      chooseDateType: 1,
      dateShow: false,
      date: Number(new Date()),
    };
  },
  onLoad(e) {
    this.getHotSearch();
    //获取历史搜索记录
    this.historyList = uni.getStorageSync('acctHistoryList') || [];
  },
  onShow() { },
  methods: {
    //请求热门搜索
    getHotSearch() {
      getAcctHotSearch().then(res => {
        if (res.code == 1) {
          this.hotSearchList = res.data;
          this.hotPlaceholderStr = res.data[0].content
        }
      });
    },

    //删除搜索历史记录
    deleteHistorySearch() {
      uni.removeStorageSync('acctHistoryList');
      this.historyList = [];
    },

    //点击搜索
    clickSearch(value) {
      this.page = 1;
			this.pageDataList = [];
			this.dataList = []
			this.total = 0
			this.searchFun(value)
    },

    //搜索
    searchFun(value) {
      let params = {
        query: value,
        retrieveFields: this.chooseId,
        page: this.page,
        pageSize: this.pageSize,
        filter: {},
      };
      this.keyword = value;
      let historyList = this.historyList;
      //判断数组retrieveFields中是否包含push_date
      if (params.retrieveFields.includes("push_date")) {
        params.query = "";
        if (this.startDate == "开始时间") {
          this.$tools.msg("请选择开始时间");
          return;
        } else if (this.endDate == "结束时间") {
          this.$tools.msg("请选择结束时间");
          return;
        }
        params.dateScope = {
          startDate: this.startDate,
          endDate: this.endDate,
        };
      } else {
        if (value == "" || value == undefined || value == null) {
          this.$tools.msg("请输入搜索内容");
          return;
        } else {
          if (historyList.indexOf(value) == -1) {
            historyList.unshift(value);
            if (historyList.length == 11) {
              historyList.pop(value);
            }
          }
          uni.setStorageSync("acctHistoryList", historyList);
        }
      }
      this.search(params)
    },

    //搜索
    search(params) {
      this.isLoading = true;
      // 请求数据
      getAcctSearch(params).then(res => {
        if (res.status == 0) {
          this.total = res.data.resultTotal;
          this.queryKeywords = res.data.queryKeywords;
          if (res.data.result.length >= this.pageSize) {
            this.status = 'more';
          } else {
            this.status = 'noMore';
          }
          this.searchList = res.data.result;
          this.isRefreshing = false;
          this.getPageData();
        }
      });
    },


    //json数据处理
    getPageData() {
      for (let i = 0; i < this.searchList.length; i++) {
        let item = this.searchList[i];
        readJsonFileContent(item.matchDataUrl).then(res => {
          this.dataList.push(res);
        });
      }
      setTimeout(() => {
        let arr = JSON.parse(JSON.stringify(this.dataList))
        this.dataList = arr
        this.isSearch = true;
        uni.hideLoading();
        this.isLoading = false;
      }, 300);
    },


    //下拉刷新
    reload() {
      this.page = 1;
      this.searchList = [];
      this.dataList = []
      this.isRefreshing = true;
      this.searchFun(this.keyword)
    },

    //加载更多
    loadMore() {
      if (this.status == 'more') {
        this.page++;
        this.searchFun(this.keyword)
      } else {
        this.$tools.msg('没有更多了');
      }
    },

    //跳转详情
    toInfo(e) {
      uni.navigateTo({ url: `/law/acct/det/index?id=${e.id}` })
    },


    onClick() {
      this.isShowPopup = true;
    },
    confirm(e) {
      this.chooseId = e.value[0].id;
      this.chooseLabel = e.value[0].label;
      this.isShowPopup = false;
      this.keyword = "";
      if (e.value[0].id == "push_date") {
        this.startDate = "开始时间";
        this.endDate = "结束时间";
      }
    },
    chooseDate(type) {
      this.chooseDateType = type;
      this.dateShow = true;
    },
    dateCancel() {
      this.dateShow = false;
    },
    dateConfirm(e) {
      if (this.chooseDateType == 1) {
        this.startDate = parseTimeDot(e.value, "{y}-{m}-{d}");
      } else {
        this.endDate = parseTimeDot(e.value, "{y}-{m}-{d}");
      }
      this.dateShow = false;
    },
  },
}